package cn.libiting.dateRange;

import java.util.Calendar;
import java.util.Date;

import static  cn.libiting.dateRange.utils.DateUtil.*;

/**
 * Created with IntelliJ IDEA.
 * User: wangxc
 * Date: 12-9-24
 * Time: 上午11:45
 * To change this template use File | Settings | File Templates.
 */
public class DefaultDateIntervalInterface implements DateIntervalInterface {
    @Override
    public Date startDay(Date date, DateRange type) {
        switch (type) {
            case CURRENT_MONTH: return getFirstDayOfMonth(date);
            case LAST_MONTH:  return getFirstDayOfMonth(lastMonthSameDay(date));
            case CURRENT_WEEK:  return getFirstDateOfWeek(date);
            case LAST_WEEK:  return getFirstDateOfWeek(lastWeekSameDay(date));
            case CURRENT_QUARTER: return  calCurrentQuarter().startDay();
            case LAST_QUARTER: return calCurrentQuarter().previous().startDay();

            case FIRST_QUARTER: return Quarter.FIRST.startDay();
            case SECOND_QUARTER: return  Quarter.SECOND.startDay();
            case THIRD_QUARTER: return  Quarter.THIRD.startDay();
            case FOURTH_QUARTER: return  Quarter.FOURTH.startDay();
        }

        throw new IllegalArgumentException();
    }

    @Override
    public Date endDay(Date date, DateRange type) {
        switch (type) {
            case CURRENT_MONTH: return getLastDayOfMonth(date);
            case LAST_MONTH:  return getLastDayOfMonth(lastMonthSameDay(date));
            case CURRENT_WEEK:  return getLastDateOfWeek(date);
            case LAST_WEEK:  return getLastDateOfWeek(lastWeekSameDay(date));
            case CURRENT_QUARTER: return calCurrentQuarter().endDay();
            case LAST_QUARTER:
                Quarter previous= calCurrentQuarter().previous();
                return previous.endDay(previous.dayInCurrentQuarter()); //TODO, 这个就现在来看功能上还算通过， 不过， 有些累赘。
            case FIRST_QUARTER: return  Quarter.FIRST.endDay();
            case SECOND_QUARTER: return Quarter.SECOND.endDay();
            case THIRD_QUARTER: return  Quarter.THIRD.endDay();
            case FOURTH_QUARTER: return Quarter.FOURTH.endDay();
        }

        throw new IllegalArgumentException();
    }



    private Quarter calCurrentQuarter() {
        Date now = DateRange.getDate();
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        return Quarter.valueOf(c.get(Calendar.MONTH));
    }
}
